package com.itheima.api;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mapper.UmsRoleMapper;
import com.itheima.pojo.UmsRole;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author 袁鹏
 * @date 2022-10-13-10:56
 */
@DubboService
public class UmsRoleApiImpl implements UmsRoleApi{

    @Autowired
    private UmsRoleMapper umsRoleMapper;


    /**
     * 添加新角色
     */
    @Override
    public void createRole(UmsRole role) {
        role.setCreateTime(new Date());
        umsRoleMapper.insert(role);
    }


    /**
     * 批量删除角色
     */
    @Override
    public void deleteMulti(Integer[] ids) {
        if(ids.length > 0) {
            umsRoleMapper.deleteBatchIds(Arrays.asList(ids));
        }
    }


    /**
     * 根据角色名分页获取角色列表
     */
    @Override
    public Page<UmsRole> pageRoleByName(String keyword, Integer pageNum, Integer pageSize) {
        Page<UmsRole> pageInfo = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<UmsRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(keyword), UmsRole::getName, keyword);
        return umsRoleMapper.selectPage(pageInfo, queryWrapper);
    }


    /**
     * 获取所有角色
     */
    @Override
    public List<UmsRole> list() {
        return umsRoleMapper.selectList(null);
    }


    /**
     * 修改角色
     */
    @Override
    public void update(UmsRole umsRole) {
        umsRoleMapper.updateById(umsRole);
    }

    /**
     * 根据角色id查询角色信息
     * @param roleIds
     * @return
     */
    @Override
    public List<UmsRole> getByIds(List<Long> roleIds) {
        if(CollUtil.isNotEmpty(roleIds)) {
            return umsRoleMapper.selectBatchIds(roleIds);
        }else{
            return Collections.emptyList();
        }
    }
}
